package org.kk.array;

/**
 * 最大的礼物
 * @author kk
 * @Date 2021/1/24
 */
public class MaxGrid {
    public static void main(String[] args) {
        int[][] grid = {
                {1,3,1},
                {1,5,1},
                {4,2,1}
        };
        int i = maxValue(grid);
        System.out.println(i);
    }
    /**
     * 示例
     * [
     *  [1,3,1],
     *  [1,5,1],
     *  [4,2,1]
     * ]
     * 返回: 12
     * 解释: 路径 1->3->5->2->1 可以拿到最大值
     */
    public static int maxValue(int[][] grid){
        if(grid == null || grid.length == 0 || grid[0].length == 0) return -1;
        int m = grid.length, n = grid[0].length;
        int[] dp = new int[n + 1];
        for (int i = 1; i <= m; i++){
            for (int j = 1; j <= n; j++) {
                dp[j] = Math.max(dp[j], dp[j - 1]) + grid[i -1][j-1];
            }
        }
        return dp[n];
    }
}
